#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <map>

using namespace std;

bool my_isalpha(string &word){
    for(auto it=word.begin();it!=word.end();it++){
        if(!isalpha((*it)))
            return false;
    }
    return true;
}

class Content{
public:
    
    void readFile(string &filename){
        ifstream ifs(filename);
        if(!ifs.good()){
            cerr<<" Read Fail "<<endl;
            return;
        }
        string word;

        while(ifs>>word){

            if(my_isalpha(word)){

            if((mp.find(word))!=mp.end()){
                mp.find(word)->second++;
            }else{
                mp.insert(pair<string,int>(word,1));
            }
        }
        }

        cout<<" -- Read Done -- "<<endl;

    }

    void storeFile(string &filename){
        ofstream ofs(filename);
        for(auto it=mp.begin();it!=mp.end();it++){
            ofs<<(*it).first<<" "<<(*it).second<<endl;
        }
        cout<<" -- Store Done -- "<<endl;
    }

    int get(string &word){
        if(mp.find(word)!=mp.end())
            return mp.find(word)->second;
        else
            return 0;
    }

private:
    map<string,int> mp;
};

void test(){
    Content con;
    
    string readfile="./The_Holy_Bible.txt";
    con.readFile(readfile);

    string storefile="./Bible_WordCount_2.txt";
    con.storeFile(storefile);

    string findWord="new";
    cout<<" -- "<<findWord<<" : "<< con.get(findWord)<<" times -- "<<endl;
}

int main()
{
 test();
 return 0;
}

